package com.shycloud.mido.organ.ranking;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.mido.common.core.exception.BusinessException;
import com.shycloud.mido.organ.api.entity.MemberEntity;
import com.shycloud.mido.organ.api.entity.WorksEntity;
import com.shycloud.mido.organ.api.vo.RankingVO;
import com.shycloud.mido.organ.mapper.MemberMapper;
import com.shycloud.mido.organ.mapper.WorksMapper;
import lombok.AllArgsConstructor;

import java.util.ArrayList;
import java.util.List;


@AllArgsConstructor
public abstract class Ranking {

	public abstract IPage<RankingVO> getRankingList(String username, Page page);

	public IPage<RankingVO> getOneUserWorks(String username, MemberMapper memberMapper, WorksMapper worksMapper, Page page) {

		IPage<RankingVO> finalResult = page;

		List<RankingVO> res = new ArrayList<>();

		RankingVO rankingVO = new RankingVO();

		//获取memberId
		List<MemberEntity> memberEntitys = memberMapper
				.selectList(new QueryWrapper<MemberEntity>().like("username", username));

		if (memberEntitys.size() < 1) {
			throw new BusinessException("用户不存在");
		}

		for (int i = 0; i < memberEntitys.size(); i++) {
			String memberId = memberEntitys.get(i).getId();
			String[] statusArr = new String[1];
			statusArr[0] = "1";
			//获取此用户的作品信息
			IPage<WorksEntity> myWorks = worksMapper.getMyWorks(page, memberId, statusArr);
			rankingVO.setUsername(username);
			rankingVO.setMiniStatus(memberEntitys.get(i).getMiniStatus());
			rankingVO.setCount((int) myWorks.getTotal());
			res.add(rankingVO);
			finalResult.setTotal(i + 1);
			finalResult.setRecords(res);
			finalResult.setPages(i + 1);
		}

		return finalResult;

	}

}
